module.exports = (function(){
  function _Promise(fun){
    var _this = this;
    this.item = new a.Item();
    if(fun instanceof Array){
        this.item.item = fun;
        this.item.length = fun.length;
    }else if(typeof fun == 'function'){
        this.mainFun = fun;
        this.item.item = [fun];
        this.item.length = 1;
    }
    this.then = function(thenFun){
      if(!this.item.begined) this.item._do();
      this.item._push(function(resolve){
        var rc = thenFun(_this.item.value);
        if(rc instanceof _Promise) {
          rc.mainFun(resolve);
        }else{
          resolve();
        }
      });
      return this;
    }
  }
  _Promise.all = function(promises){
    var values = [];
    var funs = [];
    for(var i = 0; i < promises.length; i ++) {
      (function(i){
        if(promises[i] instanceof _Promise && promises[i].mainFun){
          funs.push(function(resolve,value){
            values.push(value);
            promises[i].mainFun(resolve);
          })
        }else{
          funs.push(function(resolve,value){
            values.push(value);
            resolve(promises[i]);
          })
        }
      })(i)
    }
    funs.push(function(resolve,value){
      values.push(value);
      values.shift();
      resolve(values);
    })
    return new _Promise(funs);
  }
  return _Promise;
})()